package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.number.IceNumberUtils;
import cc.edt.iceutils2.string.IceStringUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.Expansion;
import com.edt.ichannel.api.entiy.local.HttpPostParamsWoPay;
import com.edt.ichannel.api.entiy.local.Staff;
import com.edt.ichannel.api.entiy.local.WebApiEntity;
import com.edt.ichannel.api.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ValidateServiceImpl implements ValidateService {
    @Resource
    private ExpansionService expansionService;
    @Resource
    private StaffService staffService;
    @Resource
    private AccountLogService accountLogService;

    @Override
    public ActionResult validateConsumeData(WebApiEntity entity) {
        ActionResult result = new ActionResult();
        if (IceStringUtils.isBlank(entity.getMechanismsid())) {
            result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
            result.setMessage("mechanismsid 节点为空");
            return result;
        }
        if (IceStringUtils.isBlank(entity.getPhone())) {
            result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
            result.setMessage("phone 节点为空");
            return result;
        }
        if (IceStringUtils.isBlank(entity.getShopname())) {
            result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
            result.setMessage("shopName 节点为空");
            return result;
        }

        result.setSuccess(CommonConstant.ACTIONRESULT_TRUE);
        result.setMessage("成功");
        return result;
    }

    @Override
    public ActionResult validatePayParam(HttpPostParamsWoPay params) {
        ActionResult result = new ActionResult();
        if (params == null) {
            result.setSuccess(false);
            result.setMessage("接收参数为空");
            return result;
        }
        if (params.getMerNo() == null || "".equals(params.getMerNo())) {
            result.setSuccess(false);
            result.setMessage("merNo 节点为空，请核对");
            return result;
        }
        if (params.getPayeeAcc() == null || "".equals(params.getPayeeAcc())) {
            result.setSuccess(false);
            result.setMessage("payeeAcc 节点为空，请核对");
            return result;
        }
        if (params.getAmount() == null || "".equals(params.getAmount())) {
            result.setSuccess(false);
            result.setMessage("amount 节点为空，请核对");
            return result;
        } else {
            if (!IceNumberUtils.isDigits(params.getAmount())) {
                result.setSuccess(false);
                result.setMessage("amount 节点为空有非数字字符");
                return result;
            }
        }
        if (params.getFee() == null || "".equals(params.getFee())) {
            result.setSuccess(false);
            result.setMessage("fee 节点为空，请核对");
            return result;
        }

        if (IceStringUtils.isBlank(params.getExpansionId())) {
            result.setSuccess(false);
            result.setMessage("expansionId 节点为空，请核对");
            return result;
        }

        result.setSuccess(true);
        result.setMessage("验证通过");
        return result;
    }

    @Override
    public ActionResult valistaPayAccount(HttpPostParamsWoPay params) {
        ActionResult result = new ActionResult();
        double unicomAccountMoney = accountLogService.getUnicomAccountMoneyByExpansionId(params.getExpansionId());
        Expansion expansion = expansionService.getExpansionById(params.getExpansionId());
        if ((expansion.getAccountMoney() - unicomAccountMoney) * 100 < IceNumberUtils.toInt(params.getAmount())) {
            result.setSuccess(false);
            result.setMessage("账户余额不足：" + expansion.getAccountMoney());
            return result;
        }
        result.setSuccess(true);
        result.setMessage("success");
        return result;
    }

    @Override
    public ActionResult validatePayParamStaff(HttpPostParamsWoPay params) {
        ActionResult result = new ActionResult();
        if (params == null) {
            result.setSuccess(false);
            result.setMessage("接收参数为空");
            return result;
        }
        if (IceStringUtils.isBlank(params.getMerNo())) {
            result.setSuccess(false);
            result.setMessage("merNo 节点为空，请核对");
            return result;
        }
        if (IceStringUtils.isBlank(params.getPayeeAcc())) {
            result.setSuccess(false);
            result.setMessage("payeeAcc 节点为空，请核对");
            return result;
        }
        if (IceStringUtils.isBlank(params.getAmount())) {
            result.setSuccess(false);
            result.setMessage("amount 节点为空，请核对");
            return result;
        } else {
            if (!IceNumberUtils.isDigits(params.getAmount())) {
                result.setSuccess(false);
                result.setMessage("amount 节点为空有非数字字符");
                return result;
            }
        }
        if (IceStringUtils.isBlank(params.getStaffId())) {
            if (IceStringUtils.isBlank(params.getFee())) {
                result.setSuccess(false);
                result.setMessage("fee 节点为空，请核对");
                return result;
            }
        }

        if (IceStringUtils.isBlank(params.getStaffId())) {
            result.setSuccess(false);
            result.setMessage("expansionId 节点为空，请核对");
            return result;
        }

        result.setSuccess(true);
        result.setMessage("验证通过");
        return result;
    }

    @Override
    public ActionResult valistaStaffAccount(HttpPostParamsWoPay params) {
        ActionResult result = new ActionResult();
        Staff staff = staffService.getStaffById(params.getStaffId());
        if (staff.getAccountMoney() * 100 < IceNumberUtils.toInt(params.getAmount())) {
            result.setSuccess(false);
            result.setMessage("拓展人账户余额不足：" + staff.getAccountMoney());
            return result;
        }
        result.setSuccess(true);
        result.setMessage("success");
        return result;
    }
}
